package com.dime.physical.service.impl;

import com.dime.physical.domain.*;
import com.dime.physical.domain.dto.InquiryDto;
import com.dime.physical.mapper.*;
import com.dime.physical.service.IDimeInquiryService;
import com.dime.physical.service.IDimePhysicalregisterService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class DimeInquiryServiceImpl implements IDimeInquiryService {

    @Autowired
    private DimePhysicalregisterMapper dimePhysicalregisterMapper;

    @Autowired
    private DimeInquiryitemsMapper dimeInquiryitemsMapper;


    @Autowired
    private DimeInquirydiseaseMapper dimeInquirydiseaseMapper;

    @Autowired
    private DimeInquirychronicoccupationalhistoryMapper dimeInquirychronicoccupationalhistoryMapper;


    @Autowired
    private DimeInquirycurrentdiseasesMapper dimeInquirycurrentdiseasesMapper;

    @Autowired
    private DimeInquiryoccupationsMapper dimeInquiryoccupationsMapper;

    @Autowired
    private DimeInquiryradiationotherMapper dimeInquiryradiationotherMapper;

    @Autowired
    private DimeInquirysignMapper dimeInquirysignMapper;

    @Autowired
    private DimeInquirysymptomMapper dimeInquirysymptomMapper;

    @Autowired
    private DimePhysicalflowMapper dimePhysicalflowMapper;

    @Autowired
    private DimeInquiryradiationoccupationsMapper dimeInquiryradiationoccupationsMapper;

 @Autowired
 private IDimePhysicalregisterService dimePhysicalregisterService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int Saveinquiry(InquiryDto inquiryDto) {

        int rows = 0;
        DimePhysicalregister dimePhysicalregister = dimePhysicalregisterMapper.selectDimePhysicalregisterById(inquiryDto.getId());

        if (Integer.parseInt(dimePhysicalregister.getCompletStatus()) > 1) {
            throw new ServiceException("该人员已主检！");
        }
        //保存 inquiryitems
        if (inquiryDto.inquiryitems != null) {
            rows += SaveinquiryItems(inquiryDto.inquiryitems, dimePhysicalregister);
        }

        //保存 inquirydisease
        if (inquiryDto.inquirydisease != null && inquiryDto.inquirydisease.stream().count() > 0) {
            rows += Saveinquirydisease(inquiryDto.inquirydisease, dimePhysicalregister);
        }

        //保存 inquirychronicoccupationalhistory
        if (inquiryDto.inquirychronicoccupationalhistory != null && inquiryDto.inquirychronicoccupationalhistory.size() > 0) {
            rows += Saveinquirychronicoccupationalhistory(inquiryDto.inquirychronicoccupationalhistory, dimePhysicalregister);
        }

        //保存 inquirycurrentdiseases
        if (inquiryDto.inquirycurrentdiseases != null && inquiryDto.inquirycurrentdiseases.size() > 0) {
            rows += Saveinquirycurrentdiseases(inquiryDto.inquirycurrentdiseases, dimePhysicalregister);
        }

        //保存 inquiryoccupations
        if (inquiryDto.inquiryoccupations != null && inquiryDto.inquiryoccupations.size() > 0) {
            rows += SaveDimeInquiryoccupations(inquiryDto.inquiryoccupations, dimePhysicalregister);
        }

        //保存  inquiryradiationother
        if (inquiryDto.inquiryradiationother != null) {
            rows += Saveinquiryradiationother(inquiryDto.inquiryradiationother, dimePhysicalregister);
        }

        //保存  inquirysign
        if (inquiryDto.inquirysign != null) {
            rows += Saveinquirysign(inquiryDto.inquirysign, dimePhysicalregister);
        }

        //保存 inquirysymptom
        if (inquiryDto.inquirysymptom != null && inquiryDto.inquirysymptom.size() > 0) {
            rows += Saveinquirysymptom(inquiryDto.inquirysymptom, dimePhysicalregister);
        }


        //保存 inquiryradiationoccupations
        if (inquiryDto.inquiryradiationoccupations != null && inquiryDto.inquiryradiationoccupations.size() > 0) {
            rows += Saveinquiryradiationoccupations(inquiryDto.inquiryradiationoccupations, dimePhysicalregister);
        }


        DimePhysicalflow searchflow = new DimePhysicalflow();
        searchflow.setPhysicalRegisterId(dimePhysicalregister.getId());
        searchflow.setType("05");


        DimePhysicalflow dimePhysicalflow = dimePhysicalflowMapper.selectDimePhysicalflowList(searchflow).stream().findFirst().orElse(null);
        if (dimePhysicalflow != null) {
            dimePhysicalflow.setFlowInputDate(new Date());
            dimePhysicalflow.setFlowStatus(1L);
            dimePhysicalflow.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
            dimePhysicalflow.setUpdateTime(new Date());
            rows += dimePhysicalflowMapper.updateDimePhysicalflow(dimePhysicalflow);
        }

        if (inquiryDto.getStartworkDate() != null) {
            dimePhysicalregister.setStartworkDate(inquiryDto.getStartworkDate());
        }
        if (inquiryDto.getTouharmDate() != null) {
            dimePhysicalregister.setTouharmDate(inquiryDto.getTouharmDate());
        }

        if (inquiryDto.getEmpNo() != null) {
            dimePhysicalregister.setEmpNo(inquiryDto.getEmpNo());
        }

        if (inquiryDto.getDept() != null) {
            dimePhysicalregister.setDept(inquiryDto.getDept());
        }

        if (inquiryDto.getJobType() != null) {
            dimePhysicalregister.setJobType(inquiryDto.getJobType());
        }



       // dimePhysicalregisterMapper.updateDimePhysicalregister(dimePhysicalregister);


        //更新主表标记
        dimePhysicalregisterService.UpdatePhysicalRegistercompletStatus(dimePhysicalregister);


        return rows;

    }


    @Override
    public InquiryDto inquiryDto(String checkcdoe) {

        DimePhysicalregister dimePhysicalregister =dimePhysicalregisterMapper.selectDimePhysicalregisterByVid(checkcdoe);

//        if (Objects.equals(dimePhysicalregister.getCompletStatus(), "2")) {
//            throw new ServiceException("人员已主检！");
//        }

        if (dimePhysicalregister != null) {

            InquiryDto inquiryDto = adapter(dimePhysicalregister);

            //流程表
            //获取流程表
            DimePhysicalflow dimePhysicalflow = dimePhysicalflowMapper.selectDimePhysiacaflowByPhysicalId(dimePhysicalregister.getId()).stream().filter(x -> Objects.equals(x.getType(), "05")).findFirst().orElse(null);
            if (dimePhysicalflow == null) {
                return null;
                //throw new ServiceException("未获取到问诊流程表！");
            }

            inquiryDto.setDimePhysicalflow(dimePhysicalflow);

            List<DimeInquirychronicoccupationalhistory> dimeInquirychronicoccupationalhistorys = dimeInquirychronicoccupationalhistoryMapper.selectDimeInquirychronicoccupationalhistoryByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquirychronicoccupationalhistorys != null) {
                inquiryDto.setInquirychronicoccupationalhistory(dimeInquirychronicoccupationalhistorys);
            }

            DimeInquiryitems dimeInquiryitems = dimeInquiryitemsMapper.selectDimeInquiryitemsByPhysicalId(dimePhysicalregister.getId());

            if (dimeInquiryitems != null) {
                inquiryDto.setInquiryitems(dimeInquiryitems);
            }

            List<DimeInquirydisease> dimeInquirydiseaseList = dimeInquirydiseaseMapper.selectDimeInquirydiseaseByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquirydiseaseList != null) {
                inquiryDto.setInquirydisease(dimeInquirydiseaseList);
            }

            List<DimeInquiryoccupations> dimeInquiryoccupationsList = dimeInquiryoccupationsMapper.selectDimeInquiryoccupationsByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquiryoccupationsList != null) {
                inquiryDto.setInquiryoccupations(dimeInquiryoccupationsList);
            }

            List<DimeInquiryradiationoccupations> dimeInquiryradiationoccupationsList = dimeInquiryradiationoccupationsMapper.selectDimeInquiryradiationoccupationsByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquiryradiationoccupationsList != null) {
                inquiryDto.setInquiryradiationoccupations(dimeInquiryradiationoccupationsList);
            }

            DimeInquiryradiationother dimeInquiryradiationother = dimeInquiryradiationotherMapper.selectDimeInquiryradiationotherByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquiryradiationother != null) {
                inquiryDto.setInquiryradiationother(dimeInquiryradiationother);
            }

            DimeInquirysign dimeInquirysign = dimeInquirysignMapper.selectDimeInquirysignByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquirysign != null) {
                inquiryDto.setInquirysign(dimeInquirysign);
            }

            List<DimeInquirysymptom> dimeInquirysymptomList = dimeInquirysymptomMapper.selectDimeInquirysymptomByPhysicalId(dimePhysicalregister.getId());
            if (dimeInquirysymptomList != null) {
                inquiryDto.setInquirysymptom(dimeInquirysymptomList);
            }

            return inquiryDto;
        }

        return null;
    }

    @Override
    public int Revokeinquiry(DimePhysicalregister dimePhysicalregister) {


        if (Objects.equals(dimePhysicalregister.getCompletStatus(), "2")) {
            throw new ServiceException("人员已主检！");
        }


        int rows=0;
        DimePhysicalflow searchflow=new DimePhysicalflow();
        searchflow.setPhysicalRegisterId(dimePhysicalregister.getId());
        searchflow.setType("05");


        DimePhysicalflow dimePhysicalflow=dimePhysicalflowMapper.selectDimePhysicalflowList(searchflow).stream().findFirst().orElse(null);
       //流程表
        if(dimePhysicalflow!=null)
        {
            dimePhysicalflow.setFlowStatus(0L);
            dimePhysicalflow.setFlowInputDate(null);
            dimePhysicalflow.setUpdateBy("");
            dimePhysicalflow.setUpdateTime(null);
            rows +=  dimePhysicalflowMapper.updateDimePhysicalflow(dimePhysicalflow);
        }

        //inquiryitems
        DimeInquiryitems inquiryitem = dimeInquiryitemsMapper.selectDimeInquiryitemsByPhysicalId(dimePhysicalregister.getId());

        if (inquiryitem != null)
            dimeInquiryitemsMapper.deleteDimeInquiryitemsById(inquiryitem.getId());

        //dimeInquirydiseases
        List<DimeInquirydisease> dimeInquirydiseases = dimeInquirydiseaseMapper.selectDimeInquirydiseaseByPhysicalId(dimePhysicalregister.getId());

        if (dimeInquirydiseases != null && dimeInquirydiseases.size() > 0) {
            rows += dimeInquirydiseaseMapper.deleteDimeInquirydiseaseByPhysicalId(dimePhysicalregister.getId());
        }

        //inquirychronicoccupationalhistory
        List<DimeInquirychronicoccupationalhistory>   inquirychronicoccupationalhistory1s = dimeInquirychronicoccupationalhistoryMapper.selectDimeInquirychronicoccupationalhistoryByPhysicalId(dimePhysicalregister.getId());

        if (inquirychronicoccupationalhistory1s != null) {
            rows += dimeInquirychronicoccupationalhistoryMapper.deleteDimeInquirychronicoccupationalhistoryByPhysicalId(dimePhysicalregister.getId());
        }

        //inquirycurrentdiseases
        List<DimeInquirycurrentdiseases> inquirycurrentdiseases1 = dimeInquirycurrentdiseasesMapper.selectDimeInquirycurrentdiseasesByPhysicalId(dimePhysicalregister.getId());
        if (inquirycurrentdiseases1 != null && inquirycurrentdiseases1.size() > 0) {
            rows += dimeInquirycurrentdiseasesMapper.deleteDimeInquirycurrentdiseasesByPhysicalId(dimePhysicalregister.getId());
        }

        //inquiryoccupations
        List<DimeInquiryoccupations> inquiryoccupations1 = dimeInquiryoccupationsMapper.selectDimeInquiryoccupationsByPhysicalId(dimePhysicalregister.getId());

        if (inquiryoccupations1 != null && inquiryoccupations1.size() > 0) {
            rows += dimeInquiryoccupationsMapper.deleteDimeInquiryoccupationsByPhysicalId(dimePhysicalregister.getId());
        }

        //inquiryradiationother

        DimeInquiryradiationother inquiryradiationother1 = dimeInquiryradiationotherMapper.selectDimeInquiryradiationotherByPhysicalId(dimePhysicalregister.getId());
        if (inquiryradiationother1 != null) {
            rows +=  dimeInquiryradiationotherMapper.deleteDimeInquiryradiationotherById(inquiryradiationother1.getId());
        }

        //inquirysign
        DimeInquirysign inquirysign1 = dimeInquirysignMapper.selectDimeInquirysignByPhysicalId(dimePhysicalregister.getId());
        if (inquirysign1 != null) {
            rows +=  dimeInquirysignMapper.deleteDimeInquirysignById(inquirysign1.getId());
        }

        //inquirysymptoms
        List<DimeInquirysymptom> inquirysymptoms1 = dimeInquirysymptomMapper.selectDimeInquirysymptomByPhysicalId(dimePhysicalregister.getId());
        if (inquirysymptoms1 != null && inquirysymptoms1.size() > 0) {

            rows += dimeInquirysymptomMapper.deleteDimeInquirysymptomByPhysicalId(dimePhysicalregister.getId());
        }

        //inquiryradiationoccupations
        List<DimeInquiryradiationoccupations> inquiryradiationoccupations1 = dimeInquiryradiationoccupationsMapper.selectDimeInquiryradiationoccupationsByPhysicalId(dimePhysicalregister.getId());
        if (inquiryradiationoccupations1 != null && inquiryradiationoccupations1.size() > 0) {
            rows += dimeInquiryradiationoccupationsMapper.deleteDimeInquiryradiationoccupationsByPhysicalId(dimePhysicalregister.getId());
        }



        //撤销完成标记
        dimePhysicalregister.setCompletStatus("0");
        rows+=dimePhysicalregisterMapper.updateDimePhysicalregister(dimePhysicalregister);

        return rows;



    }

    private  static  InquiryDto adapter(DimePhysicalregister dimePhysicalregister)
    {
        InquiryDto inquiryDto = new InquiryDto();

      //  inquiryDto.setHeadUrl(dimePhysicalregister.getHeadUrl());
        inquiryDto.setHeadpicture(dimePhysicalregister.getHeadpicture());
        inquiryDto.setNation(dimePhysicalregister.getNation());
        inquiryDto.setId(dimePhysicalregister.getId());
        inquiryDto.setCheckCode(dimePhysicalregister.getCheckCode());
        inquiryDto.setIdNo(dimePhysicalregister.getIdNo());
        inquiryDto.setName(dimePhysicalregister.getName());
        inquiryDto.setPersonType(dimePhysicalregister.getPersonType());
        inquiryDto.setCheckType(dimePhysicalregister.getCheckType());
        inquiryDto.setIdNoType(dimePhysicalregister.getIdNoType());
        inquiryDto.setSex(dimePhysicalregister.getSex());
        inquiryDto.setAge(dimePhysicalregister.getAge());
        inquiryDto.setMarriage(dimePhysicalregister.getMarriage());
        inquiryDto.setPhone(dimePhysicalregister.getPhone());
        inquiryDto.setAddress(dimePhysicalregister.getAddress());
        inquiryDto.setBirthday(dimePhysicalregister.getBirthday());
        inquiryDto.setBasePackageId(dimePhysicalregister.getBasePackageId());
        inquiryDto.setPackageName(dimePhysicalregister.getPackageName());
        inquiryDto.setBaseCompanyId(dimePhysicalregister.getBaseCompanyId());
        inquiryDto.setCompanyName(dimePhysicalregister.getCompanyName());
        inquiryDto.setRegisterDate(dimePhysicalregister.getRegisterDate());
        inquiryDto.setCheckDate(dimePhysicalregister.getCheckDate());
        inquiryDto.setChannel(dimePhysicalregister.getChannel());
        inquiryDto.setReportPrintDate(dimePhysicalregister.getReportPrintDate());
        inquiryDto.setMainCheckDate(dimePhysicalregister.getMainCheckDate());
        inquiryDto.setReportPrinter(dimePhysicalregister.getReportPrinter());
        inquiryDto.setJobType(dimePhysicalregister.getJobType());
        inquiryDto.setEmpNo(dimePhysicalregister.getEmpNo());
        inquiryDto.setDept(dimePhysicalregister.getDept());
        inquiryDto.setWorkStatus(dimePhysicalregister.getWorkStatus());
        inquiryDto.setCompletStatus(dimePhysicalregister.getCompletStatus());
        inquiryDto.setHarmfacter(dimePhysicalregister.getHarmfacter());
        inquiryDto.setPassword(dimePhysicalregister.getPassword());
        inquiryDto.setWorkYears(dimePhysicalregister.getWorkYears());
        inquiryDto.setWorkMonths(dimePhysicalregister.getWorkMonths());
        inquiryDto.setTouchYears(dimePhysicalregister.getTouchYears());
        inquiryDto.setTouchMonths(dimePhysicalregister.getTouchMonths());
        inquiryDto.setNeedAsk(dimePhysicalregister.getNeedAsk());
        inquiryDto.setReCheckTag(dimePhysicalregister.getReCheckTag());
        inquiryDto.setLastCheckCode(dimePhysicalregister.getLastCheckCode());
        inquiryDto.setCombLack(dimePhysicalregister.getCombLack());
        inquiryDto.setSocialCode(dimePhysicalregister.getSocialCode());
        inquiryDto.setServiceUid(dimePhysicalregister.getServiceUid());
        inquiryDto.setSoaOrderNo(dimePhysicalregister.getSoaOrderNo());
        inquiryDto.setPlatformTag(dimePhysicalregister.getPlatformTag());
        inquiryDto.setCheckResult(dimePhysicalregister.getCheckResult());
        inquiryDto.setMainConclusion(dimePhysicalregister.getMainConclusion());
        inquiryDto.setHealthSuggestion(dimePhysicalregister.getHealthSuggestion());
        inquiryDto.setMainCheckDoctor(dimePhysicalregister.getMainCheckDoctor());
        inquiryDto.setTakeReportType(dimePhysicalregister.getTakeReportType());
        inquiryDto.setIsValid(dimePhysicalregister.getIsValid());
        inquiryDto.setShopCode(dimePhysicalregister.getShopCode());
        inquiryDto.setTouchHarms(dimePhysicalregister.getTouchHarms());
        inquiryDto.setNationality(dimePhysicalregister.getNationality());
        inquiryDto.setRegistertype(dimePhysicalregister.getRegistertype());
        inquiryDto.setStartworkDate(dimePhysicalregister.getStartworkDate());
        inquiryDto.setTouharmDate(dimePhysicalregister.getTouharmDate());
        inquiryDto.setSearchValue(dimePhysicalregister.getSearchValue());
        inquiryDto.setCreateBy(dimePhysicalregister.getCreateBy());
        inquiryDto.setCreateTime(dimePhysicalregister.getCreateTime());
        inquiryDto.setUpdateBy(dimePhysicalregister.getUpdateBy());
        inquiryDto.setUpdateTime(dimePhysicalregister.getUpdateTime());
        inquiryDto.setRemark(dimePhysicalregister.getRemark());

        return inquiryDto;


    }





    private int Saveinquirysymptom(List<DimeInquirysymptom> inquirysymptoms, DimePhysicalregister dimePhysicalregister) {

        int rows = 0;
        List<DimeInquirysymptom> inquirysymptoms1 = dimeInquirysymptomMapper.selectDimeInquirysymptomByPhysicalId(dimePhysicalregister.getId());
        if (inquirysymptoms1 != null && inquirysymptoms1.size() > 0) {

            dimeInquirysymptomMapper.deleteDimeInquirysymptomByPhysicalId(dimePhysicalregister.getId());
        }
        for (DimeInquirysymptom inquirysymptom : inquirysymptoms) {


            inquirysymptom.setPhysicalRegisterId(dimePhysicalregister.getId());
            inquirysymptom.setId(IdUtils.fastUUID());
            inquirysymptom.setCreateTime(DateUtils.getNowDate());
            inquirysymptom.setCreateBy(SecurityUtils.getLoginUser().getUsername());

            rows += dimeInquirysymptomMapper.insertDimeInquirysymptom(inquirysymptom);
        }
        return rows;

    }

    private int Saveinquirysign(DimeInquirysign inquirysign, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;
        DimeInquirysign inquirysign1 = dimeInquirysignMapper.selectDimeInquirysignByPhysicalId(dimePhysicalregister.getId());
        if (inquirysign1 != null) {
            dimeInquirysignMapper.deleteDimeInquirysignById(inquirysign1.getId());
        }

        inquirysign.setPhysicalRegisterId(dimePhysicalregister.getId());
        inquirysign.setId(IdUtils.fastUUID());
        inquirysign.setCreateTime(DateUtils.getNowDate());
        inquirysign.setCreateBy(SecurityUtils.getLoginUser().getUsername());

        rows += dimeInquirysignMapper.insertDimeInquirysign(inquirysign);


        return rows;
    }

    private int Saveinquiryradiationother(DimeInquiryradiationother inquiryradiationother, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;

        DimeInquiryradiationother inquiryradiationother1 = dimeInquiryradiationotherMapper.selectDimeInquiryradiationotherByPhysicalId(dimePhysicalregister.getId());
        if (inquiryradiationother1 != null) {
            dimeInquiryradiationotherMapper.deleteDimeInquiryradiationotherById(inquiryradiationother1.getId());
        }
        inquiryradiationother.setPhysicalRegisterId(dimePhysicalregister.getId());
        inquiryradiationother.setId(IdUtils.fastUUID());
        inquiryradiationother.setCreateTime(DateUtils.getNowDate());
        inquiryradiationother.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        rows += dimeInquiryradiationotherMapper.insertDimeInquiryradiationother(inquiryradiationother);

        return rows;
    }

    private int SaveDimeInquiryoccupations(List<DimeInquiryoccupations> inquiryoccupations, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;

        List<DimeInquiryoccupations> inquiryoccupations1 = dimeInquiryoccupationsMapper.selectDimeInquiryoccupationsByPhysicalId(dimePhysicalregister.getId());

        if (inquiryoccupations1 != null && inquiryoccupations1.size() > 0) {
            dimeInquiryoccupationsMapper.deleteDimeInquiryoccupationsByPhysicalId(dimePhysicalregister.getId());
        }
        for (DimeInquiryoccupations dimeInquiryoccupations : inquiryoccupations) {
            dimeInquiryoccupations.setPhysicalRegisterId(dimePhysicalregister.getId());
            dimeInquiryoccupations.setId(IdUtils.fastUUID());
            dimeInquiryoccupations.setCreateTime(DateUtils.getNowDate());
            dimeInquiryoccupations.setCreateBy(SecurityUtils.getLoginUser().getUsername());

            rows += dimeInquiryoccupationsMapper.insertDimeInquiryoccupations(dimeInquiryoccupations);
        }

        return rows;
    }

    private int Saveinquirycurrentdiseases(List<DimeInquirycurrentdiseases> inquirycurrentdiseases, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;

        List<DimeInquirycurrentdiseases> inquirycurrentdiseases1 = dimeInquirycurrentdiseasesMapper.selectDimeInquirycurrentdiseasesByPhysicalId(dimePhysicalregister.getId());
        if (inquirycurrentdiseases1 != null && inquirycurrentdiseases1.size() > 0) {
            dimeInquirycurrentdiseasesMapper.deleteDimeInquirycurrentdiseasesByPhysicalId(dimePhysicalregister.getId());
        }

        for (DimeInquirycurrentdiseases dimeInquirycurrentdiseases : inquirycurrentdiseases) {

            dimeInquirycurrentdiseases.setPhysicalRegisterId(dimePhysicalregister.getId());
            dimeInquirycurrentdiseases.setId(IdUtils.fastUUID());
            dimeInquirycurrentdiseases.setCreateTime(DateUtils.getNowDate());
            dimeInquirycurrentdiseases.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            dimeInquirycurrentdiseases.setDoctorId(SecurityUtils.getLoginUser().getUserid().toString());
            dimeInquirycurrentdiseases.setDoctorName(SecurityUtils.getLoginUser().getUsername());

            rows += dimeInquirycurrentdiseasesMapper.insertDimeInquirycurrentdiseases(dimeInquirycurrentdiseases);
        }


        return rows;
    }

    private int Saveinquirychronicoccupationalhistory(List<DimeInquirychronicoccupationalhistory> inquirychronicoccupationalhistorys, DimePhysicalregister dimePhysicalregister) {
        int rows = 0;

      List<DimeInquirychronicoccupationalhistory>   inquirychronicoccupationalhistory1s = dimeInquirychronicoccupationalhistoryMapper.selectDimeInquirychronicoccupationalhistoryByPhysicalId(dimePhysicalregister.getId());

        if (inquirychronicoccupationalhistory1s != null) {
            dimeInquirychronicoccupationalhistoryMapper.deleteDimeInquirychronicoccupationalhistoryByPhysicalId(dimePhysicalregister.getId());
        }

        for (DimeInquirychronicoccupationalhistory inquirychronicoccupationalhistory : inquirychronicoccupationalhistorys) {

            inquirychronicoccupationalhistory.setPhysicalRegisterId(dimePhysicalregister.getId());
            inquirychronicoccupationalhistory.setId(IdUtils.fastUUID());
            inquirychronicoccupationalhistory.setCreateTime(DateUtils.getNowDate());
            inquirychronicoccupationalhistory.setCreateBy(SecurityUtils.getLoginUser().getUsername());

            rows += dimeInquirychronicoccupationalhistoryMapper.insertDimeInquirychronicoccupationalhistory(inquirychronicoccupationalhistory);

        }


        return rows;
    }

    private int Saveinquirydisease(List<DimeInquirydisease> inquirydisease, DimePhysicalregister dimePhysicalregister) {

        int rows = 0;

        List<DimeInquirydisease> dimeInquirydiseases = dimeInquirydiseaseMapper.selectDimeInquirydiseaseByPhysicalId(dimePhysicalregister.getId());

        if (dimeInquirydiseases != null && dimeInquirydiseases.size() > 0) {
            dimeInquirydiseaseMapper.deleteDimeInquirydiseaseByPhysicalId(dimePhysicalregister.getId());
        }

        if (inquirydisease != null && inquirydisease.size() > 0) {
            for (DimeInquirydisease dimeInquirydisease1 : inquirydisease) {
                dimeInquirydisease1.setPhysicalRegisterId(dimePhysicalregister.getId());
                dimeInquirydisease1.setId(IdUtils.fastUUID());
                dimeInquirydisease1.setCreateTime(DateUtils.getNowDate());
                dimeInquirydisease1.setCreateBy(SecurityUtils.getLoginUser().getUsername());

                rows += dimeInquirydiseaseMapper.insertDimeInquirydisease(dimeInquirydisease1);

            }
        }

        return rows;
    }

    private int SaveinquiryItems(DimeInquiryitems inquiryitems, DimePhysicalregister dimePhysicalregister) {
        int itemrows = 0;

        DimeInquiryitems inquiryitem = dimeInquiryitemsMapper.selectDimeInquiryitemsByPhysicalId(dimePhysicalregister.getId());

        if (inquiryitem != null)
            dimeInquiryitemsMapper.deleteDimeInquiryitemsById(inquiryitem.getId());


        inquiryitems.setPhysicalRegisterId(dimePhysicalregister.getId());
        inquiryitems.setId(IdUtils.fastUUID());
        inquiryitems.setCreateTime(DateUtils.getNowDate());
        inquiryitems.setCreateBy(SecurityUtils.getLoginUser().getUsername());

        itemrows = dimeInquiryitemsMapper.insertDimeInquiryitems(inquiryitems);

        return itemrows;
    }

    //inquiryradiationoccupations
    private int Saveinquiryradiationoccupations(List<DimeInquiryradiationoccupations> inquiryradiationoccupations, DimePhysicalregister dimePhysicalregister) {

        int rows = 0;

        List<DimeInquiryradiationoccupations> inquiryradiationoccupations1 = dimeInquiryradiationoccupationsMapper.selectDimeInquiryradiationoccupationsByPhysicalId(dimePhysicalregister.getId());
        if (inquiryradiationoccupations1 != null && inquiryradiationoccupations1.size() > 0) {
            dimeInquiryradiationoccupationsMapper.deleteDimeInquiryradiationoccupationsByPhysicalId(dimePhysicalregister.getId());
        }

        for (DimeInquiryradiationoccupations dimeInquiryradiationoccupation : inquiryradiationoccupations) {

            dimeInquiryradiationoccupation.setPhysicalRegisterId(dimePhysicalregister.getId());
            dimeInquiryradiationoccupation.setId(IdUtils.fastUUID());
            dimeInquiryradiationoccupation.setCreateTime(DateUtils.getNowDate());
            dimeInquiryradiationoccupation.setCreateBy(SecurityUtils.getLoginUser().getUsername());

            rows = dimeInquiryradiationoccupationsMapper.insertDimeInquiryradiationoccupations(dimeInquiryradiationoccupation);

        }
        return rows;
    }
}
